package com.scsystem.xysc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scsystem.xysc.aop.LogOperations;
import com.scsystem.xysc.mapper.AdminUserMapper;
import com.scsystem.xysc.mapper.UserMapper;
import com.scsystem.xysc.pojo.AdminUser;
import com.scsystem.xysc.pojo.User;
import com.scsystem.xysc.service.SendssY;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@RestController
@Transactional
@CrossOrigin
@Tag(name = "管理员接口")
@RequestMapping("/adminUser")
public class AdminUserController {
    @Autowired
    private StringRedisTemplate stringredis;//redis自带的注入就行
    @Autowired
    AdminUserMapper adminUserMapper;
    @Autowired
    UserMapper userMapper;
    @GetMapping("login")
    @Operation(summary = "管理登录，成功返回true。失败或者为null返回false")
    public Boolean userLog(String userName, String password, HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        if(!(StringUtils.isBlank(userName)||StringUtils.isBlank(password))){
            AdminUser adminUser = adminUserMapper.adminLogin(userName, password);
          Date date=new Date();
            if(adminUser!=null){
                int i = adminUserMapper.updateLastLoginTimeInt(date,adminUser.getAdminId());
                if(i==1){
                    session.setAttribute("userId",adminUser.getAdminId());
                    session.setAttribute("userType","admin");
                }
                return i==1;
            }

            return false;
        }else{
            return false;
        }

    }
    @GetMapping("session")
    @Operation(summary = "后端查看session")
    public Object hah(HttpServletRequest request){
        HttpSession session = request.getSession();
        Object userId = session.getAttribute("userId");
        return userId;
    }


    @GetMapping("page")
    @Operation(summary ="分页查询用户_输入想要的页码和每页数据")
    public Page<User> findAll(Integer yema,Integer count){
        Page<User> page = new Page<>(yema, count);
        Page<User> page1 = userMapper.selectPage(page, null);
        return page1;
    }
    @GetMapping("byName")
    @Operation(summary = "根据用户名查询用户")
    public User findByName(String name){
        User user = userMapper.userByName(name);
        return user;
    }
    @DeleteMapping("delete")
    @LogOperations(module = "管理员模块",operation = "输入用户id删除用户")
    @Operation(summary = "根据用户id删除用户")
    public Boolean deleteById(Integer id){
        int i = userMapper.deleteById(id);
        return i==1;
    }
   @PutMapping("update")
    @Operation(summary = "根据用户id修改")
   @LogOperations(module = "管理员模块",operation = "根据用户id修改用户信息")
    public Boolean updateBy(@RequestBody User user){
       System.out.println("得到的是"+user.toString());
       int i = userMapper.updateById(user);
       return i==1;
   }



    }

